<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Str;

class MemberController extends BaseController
{
    /**
     * request
     */
    protected $request;

    // 初始化操作
    public function __construct( Request $request )
    {
        // request
        $this->request = $request;
    }

    /**
     * 响应输出
     * @param array $data 响应数据
     * @return string
     */
    public function response( $data )
    {
        // 初始化返回数据
        $result = [ 'code' => 0, 'message' => 'ok', 'result' => $data ];

        // csrf token
        if( ! in_array( $this->request->method(), [ 'GET', 'HEAD', 'OPTIONS' ] ) && ! env( 'APP_DEBUG' ) && ! env( 'APP_DEBUG' ) )
        {
            // 生成token
            $csrf_token = Str::random( 40 );

            // 更新用户token
            Redis::hmset( 'member:' . $this->member_id, [ 'csrf_token' => $csrf_token ] );

            // 返回 token
            $result[ 'csrf_token' ] = $csrf_token;
        }

        return response()->json( $result );
    }
}
